package com.zr.repertory.warearea.service.impl;

import com.zr.config.ErrorMessage;
import com.zr.enumvo.WarehouseMarkEnum;
import com.zr.enumvo.WarehouseStatusEnum;
import com.zr.inquiry.pojo.InquiryAddVo;
import com.zr.planclass.pojo.PlanClassVo;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuilderVo;
import com.zr.util.ResultVo;
import com.zr.warehouse.mapper.WarehouseMapper;
import com.zr.warehouse.pojo.WarehouseAddVo;
import com.zr.warehouse.pojo.WarehouseSelectVo;
import com.zr.warehouse.pojo.WarehouseUpdateVo;
import com.zr.warehouse.pojo.WarehouseVo;
import com.zr.warehouse.service.WarehouseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by HXH on 2019/11/21 0021.
 */
@Service
public class WarehouseServiceImpl implements WarehouseService{
    @Autowired
    private WarehouseMapper warehouseMapper;

    //分页查询库房
    @Override
    public ResultVo queryPage(WarehouseSelectVo warehouseSelectVo) {
        //根据条件查询数量
        int count =  warehouseMapper.queryCount(warehouseSelectVo);
        //根据条件查询数据
        List<WarehouseVo> warehouseVoList = warehouseMapper.queryData(warehouseSelectVo);
        //把0，1转换成汉字
        for (WarehouseVo warehouseVo:warehouseVoList){
           warehouseVo.setWarehouseStatusName(WarehouseStatusEnum.getName(warehouseVo.getWarehouseStatus()));
        }
        //把数据包装进allRecords
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(warehouseSelectVo.getPageIndex());
        allRecords.setPageSize(warehouseSelectVo.getPageSize());
        allRecords.resetTotalNumber(count);
        allRecords.setTotalNumber(count);
        allRecords.setDataList(warehouseVoList);

        return ResultBuilderVo.success(allRecords);
    }

    //添加仓库
    @Override
    @Transactional
    public ResultVo addWarehouse(@Valid WarehouseAddVo warehouseAddVo) {
        //验证前端数据
            /*
            1.验证库房标识是否存在
            2.验证库房状态是否存在
            */
        ResultVo resultVo1 = yanzheng1(warehouseAddVo);
        if (!resultVo1.getSuccess()){
            return  resultVo1;
        }
        //入库
    WarehouseVo warehouseVo=new WarehouseVo();
        BeanUtils.copyProperties(warehouseAddVo,warehouseVo);
        warehouseVo.setCreateName("张三");
        warehouseVo.setCreateTime(new Date());
        warehouseVo.setUpdateName("小明");
        warehouseVo.setUpdateTime(new Date());

        warehouseMapper.addWarehouseVo(warehouseVo);
        return ResultBuilderVo.success();
    }

    /**
     * 修改前回显
     * @param warehouseID
     * @return
     */
    @Override
    public ResultVo beforeupdatewarehouse(Integer warehouseID) {
        WarehouseVo warehouseVo = warehouseMapper.beforeupdate(warehouseID);
        if(warehouseVo==null){
            return  ResultBuilderVo.error("500", ErrorMessage.QUERYBYWAREID);
        }
        return ResultBuilderVo.success(warehouseVo);
    }

    /**
     * 修改
     * @param warehouseAddVo
     * @return
     */

    @Transactional
    public ResultVo update(@Valid WarehouseUpdateVo warehouseUpdateVo) {
        //验证库房编码和名称
        ResultVo resultVo1 = yanzheng(warehouseUpdateVo);
        if (!resultVo1.getSuccess()){
            return  resultVo1;
        }
        //给修改人和修改时间等参数赋值
        //把左边对象中的参数值自动赋值给右边对象中的参数值
        //注意必须是相同类型和相同参数名的才能copy
        WarehouseVo warehouseVo=new WarehouseVo();
        BeanUtils.copyProperties(warehouseUpdateVo,warehouseVo);
        Date nowDate = new Date();
        warehouseVo.setUpdateName("小明");
        warehouseVo.setUpdateTime(nowDate);
        //修改语句。
        int update = warehouseMapper.update(warehouseVo);
        if (update==0){
            return ResultBuilderVo.error("500","请刷新后重新修改！");
        }
        return ResultBuilderVo.success();
    }
    //库房修改
    public ResultVo yanzheng(@Valid WarehouseUpdateVo warehouseUpdateVo) {
        //验证是否存在要修改的大类
       WarehouseVo warehouseVo=warehouseMapper.beforeupdate(warehouseUpdateVo.getWarehouseID());
        if(warehouseVo==null){
            return  ResultBuilderVo.error("500", ErrorMessage.QUERYBYWAREID);
        }
        //验证是否为禁用状态
        if(warehouseVo.getWarehouseStatus()==0){
            return  ResultBuilderVo.error("500", ErrorMessage.ZHUANGTAI);
        }
        //验证名字是否存在
        WarehouseVo ware=warehouseMapper.queryByName1(warehouseUpdateVo.getWarehouseName());
        //根据id判断是否修改的数据和查询出数据的id一致
        // 如果一致代表修改的还是原来的名称不用提示错误
        //否则提示名称已经存在！
        if (ware.getWarehouseID()!=warehouseUpdateVo.getWarehouseID()){
            return ResultBuilderVo.error("500", ErrorMessage.YIJINGCUNZAI);
        }
        return ResultBuilderVo.success();

    }


    public ResultVo yanzheng1(@Valid WarehouseAddVo warehouseAddVo) {
            /*
            1.验证库房标识是否存在
            2.验证库房状态是否存在
            */
        if (StringUtils.isEmpty(WarehouseMarkEnum.getName(warehouseAddVo.getWarehouseMarkStatus()))) {
            return ResultBuilderVo.error("库房标识不存在！");
        }
        if (StringUtils.isEmpty(WarehouseStatusEnum.getName(warehouseAddVo.getWarehouseStatus()))) {
            return ResultBuilderVo.error("库房状态不存在！");
        }
        //验证库房编码不能重复
        int countCode =  warehouseMapper.queryByWarehouseCode(warehouseAddVo.getWarehouseCode());
        if (countCode>0){
            return ResultBuilderVo.error(ErrorMessage.WAREHOUSECODEEXITS);
        }
        //验证库房名称不能重复
        int countName =  warehouseMapper.queryByWarehouseName(warehouseAddVo.getWarehouseName());
        if (countName>0){
            return ResultBuilderVo.error(ErrorMessage.WAREHOUSENAMEEXITS);
        }
        return ResultBuilderVo.success();
    }
}
